//
//  DecimalNumberTool.m
//  CBT
//
//  Created by Mr.Zhang on 2019/3/7.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "DecimalNumberTool.h"

@implementation DecimalNumberTool

+ (float)floatWithdecimalNumber:(double)num {
    return [[self decimalNumber:num] floatValue];
}

+ (double)doubleWithdecimalNumber:(double)num {
    return [[self decimalNumber:num] doubleValue];
}

+ (NSString *)stringWithDecimalNumber:(double)num {
    return [[self decimalNumber:num] stringValue];
}

+ (NSString *)stringWithDecimalString:(NSString *)string {
    return [NSString stringWithFormat:@"%@",[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@",string]]];
}

+ (NSString *)formatNumberWithString:(NSString *)string isAssets:(BOOL)assets {
    
    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:assets == YES ? 2 : 4 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *aDN = [NSDecimalNumber decimalNumberWithString:assets == YES ? [NSString stringWithFormat:@"%.2f",[string floatValue]] :  [NSString stringWithFormat:@"%.4f",[string floatValue]]];
    NSDecimalNumber *resultDN = [aDN decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    return [NSString stringWithFormat:@"%@",resultDN];
}

+ (NSDecimalNumber *)decimalNumber:(double)num {
    NSString *numString = [NSString stringWithFormat:@"%lf", num];
    return [NSDecimalNumber decimalNumberWithString:numString];
}

@end
